home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / games / mdlv13.zip / MDLSRC.ZIP / MDL_2D.CC < prev    next >
C/C++ Source or Header  |  1996-07-09  |  6KB  |  224 lines

  1. //
  2. // this file by brian martin 1996
  3. // brian@phyast.pitt.edu
  4. //
  5. // this is part of the source for the MedDLe quake model viewer/editor
  6. //
  7. //
  8. #include "mdl.h"
  9.  
  10. GrContext *BitMapContext;
  11.  
  12.  
  13. int mdl_2d(MDL_FILE *m)
  14. {
  15.     FILE *out;
  16.     int i,j,k,l;
  17.     unsigned p1,p2,p3,p4;
  18.     int ontopof=0;
  19.     int selectedx, selectedy, selectedindex;
  20.     int madechanges=0;
  21.     int loop;
  22.     int draw_triangles=1,draw_vertices=1;
  23.     int draw_3d=0;
  24.     int goto_3d=0;
  25.     int zoom_factor=2;
  26.  
  27.  
  28.  
  29.     BG_ClearScreen(0);
  30.         // make room for bitmap of skins
  31.         BitMapContext = GrCreateContext(mdl.bitmapw,mdl.bitmaph*mdl.num_items,NULL,NULL);
  32.         GrSetContext(BitMapContext);
  33.         for(k=0;k<mdl.num_items;k++)
  34.         {
  35.             for(j=0;j<mdl.bitmaph;j++)
  36.             {
  37.                 for(i=0;i<mdl.bitmapw;i++)
  38.                 {
  39.                     BG_PutPixel(i,j+k*mdl.bitmaph,mdl.bitmap[i+(j+k*mdl.bitmaph)*mdl.bitmapw]);
  40.                 }
  41.             }
  42.         }
  43.         GrSetContext(NULL);
  44.         // draw bitmap
  45.         GrBitBlt(NULL,0,0,BitMapContext,0,0,mdl.bitmapw-1,mdl.bitmaph-1,GrWRITE);
  46.         BG_MouseStatus();
  47.         //draw color map
  48. //        for(i=0; i<256; i++)
  49. //        {
  50. //            for(j=0; j<32; j++) BG_PutPixel(i,j+400,cmap[i+j*256]);
  51. //        }
  52.         int update=1;
  53.         int refresh=1;
  54.         int oldx=BG_MouseX,oldy=BG_MouseY,oldr=BG_MouseRight,oldl=BG_MouseLeft;
  55.         BG_MouseShow();
  56.         loop=1;
  57.         char cur_key;
  58.         int selected=0;
  59.         while(loop)
  60.         {
  61.             BG_MouseStatus();
  62.  
  63.             if(kbhit())
  64.             {
  65.                 if((cur_key=getch())==0x0) cur_key=getch();
  66.                  //    cur_key=getch();
  67.                 switch(cur_key)
  68.                 {
  69.                 case 27:               loop=0; draw_3d=0; break;
  70.                 case '3':            loop=0; draw_3d=1; break;
  71.                 case '1':            loop=0; draw_3d=2; break;
  72.                 case 'w':case 'W':  if(draw_triangles==0)draw_triangles=1;
  73.                                     else draw_triangles=0; break;
  74.                 case 'v':case 'V':  if(draw_vertices==0)draw_vertices=1;
  75.                                     else draw_vertices=0; break;
  76.                 case 's':case 'S':
  77.                         BG_Text("Saving",0,0,250,0);
  78.  
  79.                         if((out=fopen(mdl.filename,"wb"))==NULL)
  80.                         {
  81.                             fprintf(stderr,"can't open mdl file... bye\n");
  82.                             exit(1);
  83.                         }
  84.                         rewind(out);
  85.                         //disable file saving, but it works if uncommented
  86.                         //write_mdl(out, &mdl);
  87.                         fclose(out);
  88.                         break;
  89.  
  90.                 case ARROW_DOWN:    if(BG_MouseY<(BG_ScreenHeight-1)) {BG_MouseY++; BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  91.                 case ARROW_UP:        if(BG_MouseY>0) {BG_MouseY--;BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  92.                 case ARROW_LEFT:    if(BG_MouseX>0) {BG_MouseX--;BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  93.                 case ARROW_RIGHT:   if(BG_MouseX<(BG_ScreenWidth-1)) {BG_MouseX++; BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  94.                 }
  95.                 update=1;
  96.                 refresh=1;
  97.             }
  98.  
  99.             if((BG_MouseLeft==1)||(BG_MouseX!=oldx)||(BG_MouseY!=oldy))
  100.             {
  101.                 oldx=BG_MouseX;
  102.                 oldy=BG_MouseY;
  103.                 oldr=BG_MouseLeft;
  104.                 update=1;
  105.             }
  106.             // if something changed udate changes and redraw
  107.             if(update==1)
  108.             {
  109.                 BG_MouseHide();
  110.                 // if moving point
  111.                 if(BG_MouseLeft==1)
  112.                 {
  113.                     //point was already selected..
  114.                     if(selected==1)
  115.                     {
  116.                         refresh=1;
  117.                         // draw over old position  (and seemed pos)
  118.                         BG_PutPixel(mdl.vertex[selectedindex*3+1],mdl.vertex[selectedindex*3+2],mdl.bitmap[mdl.vertex[selectedindex*3+1]+mdl.vertex[selectedindex*3+2]*mdl.bitmapw]);
  119.                         if(mdl.vertex[selectedindex*3]==32)
  120.                             BG_PutPixel(mdl.bitmapw/2+mdl.vertex[selectedindex*3+1],mdl.vertex[selectedindex*3+2],mdl.bitmap[mdl.bitmapw/2+mdl.vertex[selectedindex*3+1]+mdl.vertex[selectedindex*3+2]*mdl.bitmapw]);
  121.                         // store the new position
  122.                         // if on seem store..
  123.                         if((mdl.vertex[selectedindex*3]==32)&&(BG_MouseX>mdl.bitmapw/2))
  124.                         {
  125.                             mdl.vertex[selectedindex*3+1]=BG_MouseX-mdl.bitmapw/2;
  126.                             mdl.vertex[selectedindex*3+2]=BG_MouseY;
  127.                         }
  128.                         // if not on seem...
  129.                         else
  130.                         {
  131.                             mdl.vertex[selectedindex*3+1]=BG_MouseX;
  132.                             mdl.vertex[selectedindex*3+2]=BG_MouseY;
  133.                         }
  134.                     }
  135.                 }
  136.                 else selected=0;
  137.                 // redraw skin
  138.                 if(refresh)
  139.                 GrBitBlt(NULL,0,0,BitMapContext,0,0,mdl.bitmapw-1,mdl.num_items*(mdl.bitmaph-1),GrWRITE);
  140.                 //draw 2d polgon mesh
  141.                 if(draw_triangles)
  142.                 {
  143.                     int x1,x2,x3;
  144.                     for(i=0;i<mdl.num_triangles;i++)
  145.                     {
  146.                         p1=mdl.triangle[i*4+1];
  147.                         p2=mdl.triangle[i*4+2];
  148.                         p3=mdl.triangle[i*4+3];
  149.                         p4=mdl.triangle[i*4];
  150.  
  151.                         x1=x2=x3=0;
  152.                         if((mdl.vertex[3*p1]==32)&&(p4==0)) x1=mdl.bitmapw/2;
  153.                         if((mdl.vertex[3*p2]==32)&&(p4==0)) x2=mdl.bitmapw/2;
  154.                         if((mdl.vertex[3*p3]==32)&&(p4==0)) x3=mdl.bitmapw/2;
  155.  
  156.                         BG_Line(mdl.vertex[p1*3+1]+x1,mdl.vertex[p1*3+2],mdl.vertex[p2*3+1]+x2,mdl.vertex[p2*3+2],8);
  157.                         BG_Line(mdl.vertex[p2*3+1]+x2,mdl.vertex[p2*3+2],mdl.vertex[p3*3+1]+x3,mdl.vertex[p3*3+2],8);
  158.                         BG_Line(mdl.vertex[p3*3+1]+x3,mdl.vertex[p3*3+2],mdl.vertex[p1*3+1]+x1,mdl.vertex[p1*3+2],8);
  159.  
  160.                     }
  161.                 }
  162.                 // check all vertices, if mouse is ontop of one and left
  163.                 // button is pushed, select point
  164.                 for(i=0;i<mdl.num_vertices;i++)
  165.                 {
  166.                     // not on seem
  167.                     if(mdl.vertex[i*3]!=32)
  168.                     {
  169.                         if((BG_MouseX==mdl.vertex[i*3+1])&&(BG_MouseY==mdl.vertex[i*3+2]))
  170.                         {
  171.                             if(selected==0)
  172.                             {
  173.                                 ontopof=1;
  174.                                 if(BG_MouseLeft){selectedindex=i;selected=1;}
  175.                             }
  176.                         }
  177.                         else ontopof=0;
  178.  
  179.                     }
  180.                     //else if on seem, check back of skin too
  181.                     else
  182.                     {
  183.                         if((BG_MouseX==mdl.bitmapw/2+mdl.vertex[i*3+1])&&(BG_MouseY==mdl.vertex[i*3+2])
  184.                         ||(BG_MouseX==mdl.vertex[i*3+1])&&(BG_MouseY==mdl.vertex[i*3+2]))
  185.                         {
  186.                             if(selected==0)
  187.                             {
  188.                                 ontopof=1;
  189.                                 if(BG_MouseLeft){selectedindex=i;selected=1; }
  190.                             }
  191.                         }
  192.                         else ontopof=0;
  193.  
  194.                     }
  195.                     // draw vertices
  196.                     if(draw_vertices)
  197.                     {
  198.                         BG_PutPixel(mdl.vertex[i*3+1],mdl.vertex[i*3+2],254-4*ontopof);
  199.                         // if seemed draw here too
  200.                         if(mdl.vertex[i*3]==32)
  201.                         BG_PutPixel(mdl.bitmapw/2+mdl.vertex[i*3+1],mdl.vertex[i*3+2],254-4*ontopof);
  202.                     }
  203.  
  204.  
  205.                 }
  206.  
  207.                 BG_MouseShow();
  208.                 update=0;
  209.                 refresh=0;
  210.             }
  211.  
  212.         }
  213.  
  214.  
  215.     GrDestroyContext(BitMapContext);
  216.  
  217.  
  218.  
  219.     if(draw_3d==0) return 0;
  220.     else if(draw_3d==1) return 1;
  221.     else return 2;  // show help screen
  222.  
  223.  
  224. }